<template>
  <div class="approval">
    <el-card class="box-card" style="margin-top: 110px;">
      <div class="title">{{$t('achievements.title1')}}</div>
      <div class="menu-search">
        <div class="menu-title">
          <!-- 账户 -->
          <div class="searchItem">
            <span>{{$t('user.account')}}</span>
            <el-input
              v-model="listQuery.username"
              size="small"
              :placeholder="$t('userManagement.inpAccount')"
            ></el-input>
          </div>
          <!-- 姓名 -->
          <div class="searchItem">
            <span>{{$t('user.name')}}</span>
            <el-input
              v-model="listQuery.name"
              size="small"
              :placeholder="$t('userManagement.inpUser')"
            ></el-input>
          </div>
          <div class="menu-title-btn">
            <button type="button" @click="search">{{$t('btn.query')}}</button>
            <button type="button" @click="reset">{{$t('btn.reset')}}</button>
          </div>
        </div>
      </div>
    </el-card>
    <el-card class="box-card" style="margin-top: 10px;margin-bottom: 20px;">
      <div style="display: flex; justify-content: space-between;">
        <div class="title">{{$t('DailyStatistics.ListDetails')}}</div>
        <div>
          <!-- 添加用户 -->
          <el-button
            type="primary"
            size="small"
            @click="handleCreate"
            v-if="userManagement_add_user"
          >{{$t('btn.addUser')}}</el-button>
          <!-- 批量添加用户 -->
          <el-button
            :disabled="username === 'admin'"
            type="primary"
            size="small"
            @click="handleBatchCreate"
            v-if="userManagement_batch_add_user"
          >{{$t('user.batchAddUser')}}</el-button>
        </div>
      </div>
      <el-table
        v-loading.body="listLoading"
        border
        stripe
        :data="tableData"
        style="width: 100%; margin-top: 5px;"
      >
        <el-table-column header-align="center" align="center" width="65" :label="$t('user.num')">
          <template slot-scope="scope">
            <span>{{scope.row.index}}</span>
          </template>
        </el-table-column>
        <!-- 姓名 -->
        <el-table-column
          header-align="center"
          align="center"
          prop="name"
          :label="$t('user.name')"
          width="180"
        ></el-table-column>
        <!-- 账户 -->
        <el-table-column
          header-align="center"
          align="center"
          prop="username"
          :label="$t('user.account')"
          width="180"
        ></el-table-column>
        <!-- 用户类型 -->
        <el-table-column 
          header-align="center"
          align="center"
          width="180" 
          :label="$t('LoanReport.customerType')" 
          prop='type'> 
          <template slot-scope="scope">
              <span v-show="scope.row.type === '1'">{{$t('user.inside')}}</span>
              <span v-show="scope.row.type === '2'">{{$t('user.external')}}</span>
          </template> 
        </el-table-column> 
        <!-- 备注 -->
        <el-table-column
          header-align="center"
          align="center"
          prop="description"
          :label="$t('user.remarks')"
          width="200"
        ></el-table-column>
        <!-- 状态 -->
        <el-table-column
          header-align="center"
          align="center"
          prop="status"
          width="160"
          :label="$t('grouping.status')"
        >
          <template slot-scope="scope">
            <el-radio-group v-model="scope.row.status" size="small" @change.native="changeStatus(scope.row)">
                <el-radio :label="1" size="mini">{{$t("grouping.enable")}}</el-radio>
                <el-radio :label="2" size="mini">{{$t("AppVersion.blockup")}}</el-radio>
            </el-radio-group>
          </template>
        </el-table-column>
        <!-- 创建时间 -->
        <el-table-column
          header-align="center"
          align="center"
          prop="crtTime"
          :label="$t('approvalDetail.smsCrtTime')"
          width="200"
        ></el-table-column>
        <!-- 最后更新时间 -->
        <el-table-column
          header-align="center"
          align="center"
          prop="updTime"
          :label="$t('wordbook.uptId')"
          width="200"
        ></el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          :label="$t('user.operation')"
          width="180"
          fixed="right"
        >
          <template slot-scope="scope">
            <!-- 重置密码 -->
            <el-button
              size="small"
              type="text"
              @click="resetPadFn(scope.row)"
              v-if="userManagement_reset_pwd"
            >{{$t('userManagement.resetPsd')}}</el-button>
            <!-- 编辑 -->
            <el-button
              :disabled="scope.row.attr1 === '1' && username !== 'admin'"
              size="small"
              type="text"
              @click="handleUpdate(scope.row)"
              v-if="userManagement_edit_user"
            >{{$t('btn.edit')}}</el-button>
            <!-- 删除 -->
            <template v-if="scope.row.attr1 === '1' && username !== 'admin'">
              <el-button
              :disabled="true"
              size="small"
              type="text"
              @click="handleDelete(scope.row)"
              v-if="userManagement_delete_user"
            >{{$t('btn.delete')}}</el-button>
            </template>
            <template v-else>
              <el-button
                size="small"
                type="text"
                @click="handleDelete(scope.row)"
                v-if="userManagement_delete_user"
                style="color: #f56c6c;"
              >{{$t('btn.delete')}}</el-button>
            </template>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination-container">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="listQuery.page"
          :page-sizes="[10, 30, 50]"
          :page-size="listQuery.limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
      </div>
    </el-card>
    <el-dialog
      :title="textMap[dialogStatus]"
      :visible.sync="dialogSendFormVisible"
    >
      <el-form :model="form" :rules="rules" ref="form" label-width="100px" label-position="left">
        <!-- 姓名 -->
        <el-form-item :label="$t('user.name')" prop="name">
          <el-input size="small" v-model="form.name" :placeholder="$t('userManagement.ruleUser')" autoComplete="off"></el-input>
        </el-form-item>
        <!-- 登录账户 -->
        <el-form-item :label="$t('userManagement.loginAccount')" prop="username">
          <el-input size="small" v-model="form.username" :placeholder="$t('userManagement.ruleAccount')" autoComplete="off" :disabled="dialogStatus=='update'"></el-input>
        </el-form-item>
        <!-- 设置密码 -->
        <el-form-item :label="$t('userManagement.setPsd')" prop="pass" v-if="dialogStatus=='create'">
          <el-input type="password" size="small" v-model="form.pass" :placeholder="$t('userManagement.ruleSetPsd')" autoComplete="off"></el-input>
        </el-form-item>
        <!-- 确认密码 -->
        <el-form-item :label="$t('userManagement.confirmPsd')" prop="password" v-if="dialogStatus=='create'">
          <el-input type="password" size="small" v-model="form.password" :placeholder="$t('userManagement.ruleConfirmPsd')" autoComplete="off"></el-input>
        </el-form-item>
        <!-- 所属组 -->
        <div class="trees">
          <el-form-item :label="$t('userManagement.subGroup')" prop="groupInfo">
            <div class="tree-select groups">
              <treeselect
                v-model="form.groupInfo"
                :placeholder="$t('rule.choose')"
                :multiple="true"
                :options="channelArr"
                :openOnFocus="true"
                :normalizer="normalizer"
                :disable-branch-nodes="true"
                :show-count="true"
                value-consists-of="LEAF_PRIORITY"
                :default-expand-level="1"
                :max-height="200"
              />
            </div>
          </el-form-item>
        </div>
        <!-- 选择角色 -->
        <el-form-item :label="$t('userManagement.chooseRole')" prop="roleInfo">
          <el-select 
          size='small' 
          style="width: 100%;"
          multiple 
          v-model='form.roleInfo' 
          filterable 
          :placeholder="$t('rule.choose')">
              <el-option
              v-for='item in roleArr'
              :key='item.id'
              :label='item.name'
              :value='item.id'>
              </el-option>
          </el-select>
        </el-form-item>
        <div style="display: flex;">
          <el-col :span="12">
            <!-- 用户类型 -->
            <el-form-item :label="$t('LoanReport.customerType')" prop="type">
              <el-radio-group v-model="form.type">
                <el-radio label="1">{{$t('user.inside')}}</el-radio>
                <el-radio label="2">{{$t('user.external')}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <!-- 状态 -->
            <el-form-item :label="$t('grouping.status')" prop="status" label-width="20px;">
              <el-radio-group v-model="form.status">
                <el-radio :label="1" size="small" :disabled="dialogStatus=='update'">{{$t('grouping.enable')}}</el-radio>
                <el-radio :label="2" size="small" :disabled="dialogStatus=='update'">{{$t('AppVersion.blockup')}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </div>
        <!-- 备注 -->
        <el-form-item :label="$t('user.remarks')" prop="description">
          <el-input
            type="textarea"
            :autosize="{ minRows: 3, maxRows: 5}"
            v-model="form.description"
            :placeholder="$t('rule.toLength')" 
            :maxlength="500"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button
          v-if="dialogStatus=='create'"
          type="primary"
          @click="create('form')"
          :loading="isDisable"
        >{{$t('btn.conserve')}}</el-button>
        <el-button
          v-if="dialogStatus=='update'"
          type="primary"
          @click="update('form')"
          :loading="isUpdate"
        >{{$t('btn.conserve')}}</el-button>
      </div>
    </el-dialog>
     <el-dialog
      :title="$t('user.batchAddUser')" 
      :visible.sync="dialogBatchVisible"
    >
      <el-form :model="batchForm" :rules="batchRules" ref="batchForm" label-width="100px" label-position="left">
        <!-- 账户前缀 -->
        <el-form-item :label="$t('userManagement.accountPrefix')" prop="preFix">
          <el-input size="small" v-model="batchForm.preFix" :placeholder="$t('userManagement.ruleAccountPrefix')"></el-input>
        </el-form-item>
        <!-- 开始编号 -->
        <el-form-item :label="$t('userManagement.startNum')" prop="startNum">
          <el-input size="small" type="number" v-model="batchForm.startNum" :placeholder="$t('user.rule2')"></el-input>
        </el-form-item>
        <!-- 结束编号 -->
        <el-form-item :label="$t('userManagement.endNum')" prop="endNum">
          <el-input size="small" type="number" v-model="batchForm.endNum" :placeholder="$t('user.rule3')"></el-input>
        </el-form-item>
        <!-- 设置密码 -->
        <el-form-item :label="$t('userManagement.setPsd')" prop="pass">
          <el-input type="password" size="small" v-model="batchForm.pass" :placeholder="$t('userManagement.ruleSetPsd')" autoComplete="off"></el-input>
        </el-form-item>
        <!-- 确认密码 -->
        <el-form-item :label="$t('userManagement.confirmPsd')" prop="password">
          <el-input type="password" size="small" v-model="batchForm.password" :placeholder="$t('userManagement.ruleConfirmPsd')" autoComplete="off"></el-input>
        </el-form-item>
        <div class="trees">
          <!-- 所属组 -->
          <el-form-item :label="$t('userManagement.subGroup')" prop="groupInfo">
            <div class="tree-select groups">
              <treeselect
                v-model="batchForm.groupInfo"
                :placeholder="$t('rule.choose')"
                :multiple="true"
                :options="channelArr"
                :openOnFocus="true"
                :normalizer="normalizer"
                :disable-branch-nodes="true"
                :show-count="true"
                :default-expand-level="1"
                :max-height="200"
              />
            </div>
          </el-form-item>
        </div>
        <!-- 选择角色 -->
        <el-form-item :label="$t('userManagement.chooseRole')" prop="roleInfo">
          <el-select 
          multiple 
          v-model='batchForm.roleInfo' 
          filterable 
          size='small' 
          :placeholder="$t('rule.choose')">
              <el-option
              v-for='item in roleArr'
              :key='item.id'
              :label='item.name'
              :value='item.id'>
              </el-option>
          </el-select>
        </el-form-item>
        <div style="display: flex;">
          <!-- 用户类型 -->
          <el-col :span="12">
            <el-form-item :label="$t('LoanReport.customerType')" prop="type">
              <el-radio-group v-model="batchForm.type">
                <el-radio label="1">{{$t('user.inside')}}</el-radio>
                <el-radio label="2">{{$t('user.external')}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <!-- 状态 -->
          <el-col :span="12">
            <el-form-item :label="$t('grouping.status')" prop="status" label-width="20px;">
              <el-radio-group v-model="batchForm.status">
                <el-radio :label="1" size="small">{{$t('grouping.enable')}}</el-radio>
                <el-radio :label="2" size="small">{{$t('AppVersion.blockup')}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </div>
        <!-- 备注 -->
        <el-form-item :label="$t('user.remarks')" prop="description">
          <el-input
            type="textarea"
            :autosize="{ minRows: 3, maxRows: 5}"
            v-model="batchForm.description"
            :placeholder="$t('rule.toLength')" 
            :maxlength="500"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button
          type="primary"
          @click="batchCreate('batchForm')"
          :loading="isBatch"
        >{{$t('btn.conserve')}}</el-button>
      </div>
    </el-dialog>
    <el-dialog
    :title="$t('userManagement.resetPsd')" 
    :visible.sync="dialogResetPsdVisible">
      <el-form :model="resetPsdForm" :rules="resetPadRules" ref="resetPsdForm" label-width="100px" label-position="left">
        <!-- 新密码 -->
        <el-form-item :label="$t('userManagement.newPsd')" prop="pass">
          <el-input type="password" v-model="resetPsdForm.pass" :placeholder="$t('userManagement.ruleNewPsd')"></el-input>
        </el-form-item>
        <!-- 确认密码 -->
        <el-form-item :label="$t('userManagement.confirmPsd')" prop="password">
          <el-input type="password" v-model="resetPsdForm.password" :placeholder="$t('userManagement.ruleSecondPsd')"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button
          type="primary"
          @click="resetPsdFn('resetPsdForm')"
        >{{$t('btn.conserve')}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import { getAllUser, addUser, addBatchUser, updateUser, updatePwd, delObj, startUpAndDown, groupTree, getRoleGroup, getGroupIds } from 'api/admin/userManagement/index';
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  components: { Treeselect },
  data() {
    // 添加 设置密码验证
    var formPass = (rule, value, callback) => {
      if (value === '' || value === undefined || value === null) {
        callback(new Error(this.$t('userManagement.ruleSetPsd')));
      } else {
        if (this.form.password !== '') {
          if (value.length < 6) {
            callback(new Error(this.$t('userManagement.minLenSix')));
          } else {
            this.$refs.form.validateField('password');
          }
        }
        callback();
      }
    };
    // 添加 确认密码验证
    var formPass2 = (rule, value, callback) => {
      if (value === '' || value === undefined || value === null) {
        callback(new Error(this.$t('userManagement.ruleConfirmPsd')));
      } else if (value !== this.form.pass) {
        callback(new Error(this.$t('userManagement.checkPsd')));
      } else {
        callback();
      }
    };
    // 批量添加 设置密码验证
    var batchPass = (rule, value, callback) => {
      if (value === '' || value === undefined || value === null) {
        callback(new Error(this.$t('userManagement.ruleSetPsd')));
      } else {
        if (this.batchForm.password !== '') {
          if (value.length < 6) {
            callback(new Error(this.$t('userManagement.minLenSix')));
          } else {
            this.$refs.batchForm.validateField('password');
          }
        }
        callback();
      }
    };
    // 批量添加 确认密码验证
    var batchPass2 = (rule, value, callback) => {
      if (value === '' || value === undefined || value === null) {
        callback(new Error(this.$t('userManagement.ruleConfirmPsd')));
      } else if (value !== this.batchForm.pass) {
        callback(new Error(this.$t('userManagement.checkPsd')));
      } else {
        callback();
      }
    };
    // 重置密码 新密码
    var resetPass = (rule, value, callback) => {
      if (value === '' || value === undefined || value === null) {
        callback(new Error(this.$t('userManagement.ruleNewPsd')));
      } else {
        if (this.resetPsdForm.password !== '') {
          if (value.length < 6) {
            callback(new Error(this.$t('userManagement.minLenSix')));
          } else {
            this.$refs.resetPsdForm.validateField('password');
          }
        }
        callback();
      }
    };
    // 重置密码 确认密码
    var resetPass2 = (rule, value, callback) => {
      if (value === '' || value === undefined || value === null) {
        callback(new Error(this.$t('userManagement.ruleSecondPsd')));
      } else if (value !== this.resetPsdForm.pass) {
        callback(new Error(this.$t('userManagement.checkPsd')));
      } else {
        callback();
      }
    };
    // 批量添加 开始编号
    var batchStart = (rule, value, callback) => {
      if (value === '' || value === undefined || value === null) {
        callback(new Error(this.$t('user.rule2')));
      } else {
        if (this.batchForm.startNum !== '') {
          if (value.length > 3) {
            callback(new Error(this.$t('userManagement.ruleNum')));
          }
          if (Number(value) > Number(this.batchForm.endNum) || Number(value) === Number(this.batchForm.endNum)) {
            callback(new Error(this.$t('userManagement.ruleStartNum')));
          } 
        }
        callback();
      }
    };
    // 批量添加 结束编号
    var batchEnd = (rule, value, callback) => {
      if (value === '' || value === undefined || value === null) {
        callback(new Error(this.$t('user.rule3')));
      } else {
        if (this.batchForm.endNum !== '') {
          if (value.length > 3) {
            callback(new Error(this.$t('userManagement.ruleNum')));
          }
          if (Number(value) < Number(this.batchForm.startNum) || Number(value) === Number(this.batchForm.startNum)) {
            callback(new Error(this.$t('userManagement.ruleEndNum')));
          } 
        }
        callback();
      }
    };
    return {
      userManagement_add_user: false,
      userManagement_batch_add_user: false,
      userManagement_reset_pwd: false,
      userManagement_edit_user: false,
      userManagement_delete_user: false,
      roleArr: [],
      dialogStatus: "",
      textMap: {
        create: this.$t("btn.addUser"),
        update: this.$t("userManagement.editUser")
      },
      channelArr: [],
      normalizer(node) {
        return {
          id: node.id,
          label: node.name,
          children: node.group }
      },
      dialogResetPsdVisible: false,
      resetPsdForm: {
        pass: undefined,
        password: undefined,
      },
      resetPadRules: {
        pass: [
          {
            validator: resetPass,
            required: true,
            trigger: 'blur'
          }
        ],
        password: [
          {
            validator: resetPass2,
            required: true,
            trigger: 'blur'
          }
        ],
      },
      batchForm: {
        status: 1,
        description: undefined,
        groupInfo: null,
        roleInfo: [],
        pass: undefined,
        password: undefined,
        startNum: undefined,
        endNum: undefined,
        preFix: undefined,
        type: '1'
      },
      batchRules: {
        preFix: [
          {
            required: true,
            message: this.$t('user.rule1'),
            trigger: 'blur'
          }
        ],
        startNum: [
          { 
            pattern: /^[0-9]\d*$/, 
            message: this.$t('user.ruleNum') 
          },
          {
            validator: batchStart,
            required: true,
            trigger: 'blur'
          }
        ],
        endNum: [
          { 
            pattern: /^[0-9]\d*$/, 
            message: this.$t('user.ruleNum') 
          },
          {
            validator: batchEnd,
            required: true,
            trigger: 'blur'
          }
        ],
        pass: [
          {
            validator: batchPass,
            required: true,
            trigger: 'blur'
          }
        ],
        password: [
          {
            validator: batchPass2,
            required: true,
            trigger: 'blur'
          }
        ],
        groupInfo: [
          {
            required: true,
            message: this.$t('userManagement.ruleSubGroup')
          }
        ],
        roleInfo: [
          {
            required: true,
            message: this.$t('userManagement.ruleChooseRole')
          }
        ],
        type: [
          {
            required: true,
            message: this.$t('rule.userType'),
            trigger: 'blur'
          }
        ]
      },
      dialogBatchVisible: false,
      form: {
        status: 1,
        description: undefined,
        groupInfo: null,
        roleInfo: [],
        pass: undefined,
        password: undefined,
        username: undefined,
        name: undefined,
        type: '1'
      },
      rules: {
        name: [
          {
            required: true,
            message: this.$t('userManagement.ruleUser'),
            trigger: 'blur'
          }
        ],
        username: [
          {
            required: true,
            message: this.$t('userManagement.ruleAccount'),
            trigger: 'blur'
          },
          {
            min: 3,
            max: 20,
            message: this.$t('rule.length'),
            trigger: 'blur'
          },
          { validator: function(rule, value, callback) {
            if (value) {
              if (/[\u4E00-\u9FA5]/g.test(value)) {
                callback(new Error('账户不能输入汉字!'));
              } else {
                callback();
              }
            }
            callback();
          }, trigger: 'blur'}
        ],
        pass: [
          {
            validator: formPass,
            required: true,
            trigger: 'blur'
          }
        ],
        password: [
          {
            validator: formPass2,
            required: true,
            trigger: 'blur'
          }
        ],
        groupInfo: [
          {
            required: true,
            message: this.$t('userManagement.ruleSubGroup')
          }
        ],
        roleInfo: [
          {
            required: true,
            message: this.$t('userManagement.ruleChooseRole')
          }
        ],
        type: [
          {
            required: true,
            message: this.$t('rule.userType'),
            trigger: 'blur'
          }
        ]
      },
      dialogSendFormVisible: false,
      listLoading: false,
      total: null,
      expDis: true,
      listQuery: {
        limit: 10,
        page: 1,
        username: '',
        name: ''
      },
      tableData: [],
      userGroupArr: [
        {
          value: "",
          label: this.$t("rule.all")
        }
      ],
      currentId: '',
      isBatch: false,
      isDisable: false,
      isUpdate: false
    };
  },
  computed: {
    ...mapGetters(["elements", "username"])
  },
  created() {
    this.userManagement_add_user = this.elements['userManagement:add_user'];
    this.userManagement_batch_add_user = this.elements['userManagement:batch_add_user'];
    this.userManagement_reset_pwd = this.elements['userManagement:reset_pwd'];
    this.userManagement_edit_user = this.elements['userManagement:edit_user'];
    this.userManagement_delete_user = this.elements['userManagement:delete_user'];
    this.getList();
  },
  methods: {
    // 所属组
    getGroup() {
      groupTree()
      .then(res => {
        this.channelArr = res;
      })
    },
    // 所属角色
    getRole() {
      getRoleGroup()
      .then(res => {
        this.roleArr = res;
      })
    },
    // 重置密码
    resetPadFn(row) {
      this.currentId = row.id;
      this.resetPsdForm = {
        pass: undefined,
        password: undefined
      };
      this.dialogResetPsdVisible = true;
    },
    // 重置密码保存
    resetPsdFn(formName) {
      const set = this.$refs;
      set[formName].validate(valid => {
        if (valid) {
          this.$confirm(this.$t('approvalDetail.cue1'), {
            confirmButtonText: this.$t('btn.confirm'),
            cancelButtonText: this.$t('btn.cancel'),
            type: "warning",
            center: true
          })
          .then(() => {
            let obj = {};
            obj.id = this.currentId;
            obj.password = this.resetPsdForm.password;
            updatePwd(obj)
              .then(res => {
                if (res.status === 200) {
                  this.dialogResetPsdVisible = false;
                  this.getList();
                  this.$notify({
                    title: this.$t('user.success'),
                    message: this.$t('department.editsuccess'),
                    type: 'success',
                    duration: 2000
                  });
                }
              });
          })
          .catch(() => {
            this.$message({
              showClose: true,
              type: "info",
              message: this.$t('approvalDetail.cue3')
            });
          });
        } else {
          return false;
        }
      });
    },
    // closeDialog(done) {
    //   done();
    //   this.$refs["form"].resetFields();
    // },
    // closeBatchDialog(done) {
    //   done();
    //   this.$refs["batchForm"].resetFields();
    // },
    resetTemp() {
      this.form = {
        status: 1,
        description: undefined,
        groupInfo: null,
        roleInfo: [],
        pass: undefined,
        password: undefined,
        username: undefined,
        name: undefined,
        type: '1'
      };
    },
    // 重置批量添加
    resetBatch() {
      this.batchForm = {
        status: 1,
        description: undefined,
        groupInfo: null,
        roleInfo: [],
        pass: undefined,
        password: undefined,
        startNum: undefined,
        endNum: undefined,
        preFix: undefined,
        type: '1'
      }
    },
    // 点击批量添加按钮
    handleBatchCreate() {
      // 所属组
      this.getGroup();
      // 所属角色
      this.getRole();
      this.resetBatch();
      this.dialogBatchVisible = true;
      this.isBatch = false;
    },
    // 批量添加保存
    batchCreate(formName) {
      const set = this.$refs;
      set[formName].validate(valid => {
        if (valid) {
          this.$confirm(this.$t('approvalDetail.cue1'), {
            confirmButtonText: this.$t('btn.confirm'),
            cancelButtonText: this.$t('btn.cancel'),
            type: "warning",
            center: true
          })
          .then(() => {
            this.isBatch = true;
            let obj = {};
            if (this.batchForm.groupInfo !== null) obj.groupInfo = this.batchForm.groupInfo.join();
            if (this.batchForm.roleInfo.length > 0) obj.roleInfo = this.batchForm.roleInfo.join();
            obj.description = this.batchForm.description;
            obj.password = this.batchForm.password;
            obj.type = this.batchForm.type;
            obj.status = this.batchForm.status;
            obj.startNum = this.batchForm.startNum;
            obj.endNum = this.batchForm.endNum;
            obj.preFix = this.batchForm.preFix;
            addBatchUser(obj)
              .then(res => {
                this.isBatch = false;
                if (res.status === 200) {
                  this.dialogBatchVisible = false;
                  this.getList();
                  this.$notify({
                    title: this.$t('user.success'),
                    message: this.$t('user.crtsuccess'),
                    type: 'success',
                    duration: 2000
                  });
                }
              });
          })
          .catch(() => {
            this.isBatch = false;
            this.$message({
              showClose: true,
              type: "info",
              message: this.$t('approvalDetail.cue3')
            });
          });
        } else {
          return false;
        }
      });
    },
    // 点击添加按钮
    handleCreate() {
      // 所属组
      this.getGroup();
      // 所属角色
      this.getRole();
      this.resetTemp();
      this.isDisable = false;
      this.dialogStatus = "create";
      this.dialogSendFormVisible = true;
    },
    // 添加保存按钮
    create(formName) {
      const set = this.$refs;
      set[formName].validate(valid => {
        if (valid) {
          this.$confirm(this.$t("loanDetail.cue1"), this.$t("user.cue2"), {
            confirmButtonText: this.$t("btn.confirm"),
            cancelButtonText: this.$t("btn.cancel"),
            type: "warning"
          }).then(() => {
            let obj = {};
            if (this.form.groupInfo !== null) obj.groupInfo = this.form.groupInfo.join();
            if (this.form.roleInfo.length > 0) obj.roleInfo = this.form.roleInfo.join();
            obj.description = this.form.description;
            obj.password = this.form.password;
            obj.username = this.form.username;
            obj.name = this.form.name;
            obj.type = this.form.type;
            obj.status = this.form.status;
            this.isDisable = true;
            addUser(obj)
            .then((res) => {
              if (res.status === 200) {
                this.isDisable = false;
                this.dialogSendFormVisible = false;
                this.getList();
                this.$notify({
                  title: this.$t('user.success'),
                  message: this.$t('user.crtsuccess'),
                  type: 'success',
                  duration: 2000
                });
              } else if (res.status === 201) {
                this.isDisable = false;
                this.$message({
                  showClose: true,
                  type: "warning",
                  message: res.message
                });
              }
            });
          }).catch(() => {
            this.isDisable = false;
            this.$message({
              showClose: true,
              type: "info",
              message: this.$t("orderQuery.cue2")
            });
          });
        } else {
          return false;
        }
      });
    },
    // 点击编辑按钮
    handleUpdate(row) {
      this.currentId = row.id;
      this.dialogStatus = "update";
      this.isUpdate = false;
      // 所属组
      this.getGroup();
      // 所属角色
      this.getRole();
      let obj = {};
      obj.userId = row.id;
      getGroupIds(obj)
      .then(res => {
        this.form.groupInfo = res.userGroup;
        this.form.roleInfo = res.roleList;
        this.form.name = row.name;
        this.form.username = row.username;
        this.form.type = row.type;
        this.form.status = row.status;
        this.form.description = row.description;
        this.dialogSendFormVisible = true;
      })
    },
    // 编辑保存按钮
    update(formName) {
      const set = this.$refs;
      set[formName].validate(valid => {
        if (valid) {
          this.$confirm(this.$t("loanDetail.cue1"), this.$t("user.cue2"), {
            confirmButtonText: this.$t("btn.confirm"),
            cancelButtonText: this.$t("btn.cancel"),
            type: "warning"
          }).then(() => {
            let obj = {};
            obj.id = this.currentId;
            if (this.form.groupInfo !== null) obj.groupInfo = this.form.groupInfo.join();
            if (this.form.roleInfo.length > 0) obj.roleInfo = this.form.roleInfo.join();
            obj.description = this.form.description;
            obj.password = this.form.password;
            obj.username = this.form.username;
            obj.name = this.form.name;
            obj.type = this.form.type;
            obj.status = this.form.status;
            this.isUpdate = true;
            updateUser(obj).then(() => {
              this.isUpdate = false;
              this.dialogSendFormVisible = false;
              this.getList();
              this.$notify({
                title: this.$t('user.success'),
                message: this.$t('menu.uptsuccess'),
                type: 'success',
                duration: 2000
              });
            });
          }).catch(() => {
            this.isUpdate = false;
            this.$message({
              showClose: true,
              type: "info",
              message: this.$t("orderQuery.cue2")
            });
          });
        } else {
          return false;
        }
      });
    },
    // 点击删除按钮
    handleDelete(row) {
     this.$confirm(this.$t('user.cue1'), this.$t('user.cue2'), {
        confirmButtonText: this.$t('btn.confirm'),
        cancelButtonText: this.$t('btn.cancel'),
        type: 'warning'
      })
        .then(() => {
          delObj(row.id)
            .then(() => {
              this.$notify({
                title: this.$t('user.success'),
                message: this.$t('user.delsuccess'),
                type: 'success',
                duration: 2000
              });
              this.getList();
            });
        }).catch(() => {
        this.$message({
          showClose: true,
          type: "info",
          message: this.$t("orderQuery.cue2")
        });
      });
    },
    // 修改状态
    changeStatus(row) {
      let obj = {};
      obj.id = row.id;
      obj.status = row.status;
      startUpAndDown(obj).then(res => {
        if (res.status === 200) {
          this.$message({
            showClose: true,
            type: "success",
            message: this.$t("approvalDispatch.opersuccess")
          });
          this.getList();
        } else {
          this.$message({
            showClose: true,
            type: "warning",
            message: res.message
          });
          this.getList();
        }
      });
    },
    getList() {
      this.listLoading = true;
      getAllUser(this.listQuery)
      .then(res => {
        if (res.status === 200) {
          this.listLoading = false;
          this.tableData = res.data.rows;
          this.tableData.forEach((item, index) => {
            item.index = index + (this.listQuery.page - 1) * this.listQuery.limit + 1
          })
          this.total = Number(res.data.total);
        }
      })
    },
    //查询
    search() {
      this.listQuery.page = 1;
      this.getList();
    },
    //重置
    reset() {
      this.listQuery.limit = 10;
      this.listQuery.page = 1;
      this.listQuery.username = "";
      this.listQuery.name = "";
    },
    handleSizeChange(val) {
      this.listQuery.limit = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getList();
    }
  }
};
</script>

<style scoped lang='scss'>
.box-card {
  width: 95%;
  margin-left: 2.5%;
  margin-right: 2.5%;
}
.title {
  line-height: 30px;
  font-size: 16px;
  display: flex;
  align-items: center;
  font-weight: bold;
}
.title:before {
  display: block;
  content: "";
  width: 3px;
  height: 16px;
  background-color: #309afe;
}
.title:before {
  margin-right: 8px;
}
::before {
  box-sizing: inherit;
  -webkit-tap-highlight-color: transparent;
}
.menu-title {
  width: 100%;
  box-sizing: border-box;
  background: #ffffff;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  .searchItem {
    display: flex;
    align-items: center;
    padding: 10px 0;
    span {
      display: inline-block;
      width: 100px;
      text-align: right;
      font-size: 12px;
      color: #5f5f61;
      padding-right: 5px;
    }
    div:nth-child(2) {
      width: 200px;
    }
  }
  .searchItem-date {
    display: flex;
    align-items: center;
    padding: 10px 0;
    b {
      margin: 0 10px;
      font-weight: normal;
      font-size: 12px;
      color: #5f5f61;
    }
    div {
      width: 150px;
    }
  }
}
.menu-title-btn {
  padding: 10px 0;
  box-sizing: border-box;
  margin-left: 20px;
  button {
    padding: 0 20px;
    height: 28px;
    background: #20a0ff;
    color: #fff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
    margin-left: 10px;
    &:nth-child(1) {
      margin-left: 0;
    }
    &:nth-child(2) {
      background: #ebf5ff;
      color: #20a0ff;
      border: 1px solid #abd5ff;
    }
  }
  button[disabled] {
    padding: 0 20px;
    height: 28px;
    background: #c1e0ff;
    color: #fafbff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
  }
}
.menu-list {
  padding: 20px 0;
  box-sizing: border-box;
  button {
    padding: 0 20px;
    height: 28px;
    background: #20a0ff;
    color: #fff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
    margin-left: 10px;
    &:nth-child(1) {
      margin-left: 0;
    }
  }
  button[disabled] {
    padding: 0 20px;
    height: 28px;
    background: #c1e0ff;
    color: #fafbff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
  }
}
.tree-select /deep/.vue-treeselect__control {
  padding-left: 5px;
  padding-right: 5px;
  display: table;
  table-layout: fixed;
  width: 100%;
  height: 30px !important;
  border: 1px solid #bfcbd9;
  border-radius: 5px;
  background: #fff;
  transition-duration: 200ms;
  transition-property: border-color, box-shadow, width, height, background-color,
    opacity;
  transition-timing-function: cubic-bezier(0.215, 0.61, 0.355, 1);
}
.tree-select /deep/.vue-treeselect__placeholder,
.vue-treeselect__single-value {
  color: #97a8be;
  font-size: 13px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  padding-left: 5px;
  padding-right: 5px;
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  line-height: 34px !important;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
  pointer-events: none;
}
.tree-select {
  width: 100%;
  height: 34px;
}
.tree-select
  /deep/.vue-treeselect--searchable:not(.vue-treeselect--disabled)
  .vue-treeselect__value-container {
  cursor: pointer;
}
.tree-select /deep/.vue-treeselect__menu-container {
  position: absolute;
  left: 0;
  width: 100%;
  overflow: visible;
  transition: 0s;
  max-height: 274px;
  z-index: 2000 !important;
}
.tree-select /deep/.vue-treeselect {
  position: absolute;
  text-align: left;
  width: 100%;
}
.tree-select /deep/.vue-treeselect__label {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    display: table-cell;
    padding-left: 5px;
    max-width: 100%;
    vertical-align: middle;
    cursor: inherit;
    font-size: 12px;
    color: #5f5f61;
}
.tree-select /deep/label {
  font-weight: normal!important;
}
.groups {
  width: 100%;
  height: 34px;
}
.groups /deep/.vue-treeselect {
  position: absolute;
  text-align: left;
  width: 100%;
  height: 34px;
}
.trees /deep/.el-form-item__content {
  line-height: 20px !important;
  position: relative;
  font-size: 14px;
}
.trees /deep/.vue-treeselect__menu-container {
  min-height: 34px;
}
.trees /deep/.vue-treeselect__multi-value {
  min-height: 34px;
  margin-bottom: 0;
}
.trees /deep/.vue-treeselect__multi-value-item {
  cursor: pointer;
  display: inline-table;
  background: #e3f2fd;
  padding: 0;
  border: 1px solid transparent;
  border-radius: 2px;
  color: #039be5;
  font-size: 12px;
  vertical-align: top;
}
.groups /deep/.vue-treeselect__placeholder, .vue-treeselect__single-value {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    padding-left: 5px;
    padding-right: 5px;
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    line-height: 34px;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
    pointer-events: none;
}
</style>
